﻿/*
Copyright (C) 2008, 2009 Henrik Naess

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Data;

namespace MatrixPlugin.Utils
{
    public class DataTableUtility
    {
        public static int CountEmptyRows(DataTable table)
        {
            int emptyRows = 0;
            for (int rowIndex = table.Rows.Count - 1; rowIndex >= 0; rowIndex--)
                if (RowIsEmpty(table.Rows[rowIndex]))
                    emptyRows++;

            return emptyRows;
        }

        public static int CountEmptyColumns(DataTable table)
        {
            int emptyRows = 0;
            for (int columnIndex = table.Columns.Count - 1; columnIndex >= 0; columnIndex--)
                if (ColumnIsEmpty(table, columnIndex))
                    emptyRows++;

            return emptyRows;
        }

        public static bool ColumnIsEmpty(DataTable table, int columnIndex)
        {
            foreach (DataRow row in table.Rows)
                if (!String.IsNullOrEmpty(row.ItemArray[columnIndex] as String))
                    return false;

            return true;
        }

        public static bool RowIsEmpty(DataRow row)
        {
            foreach (object o in row.ItemArray)
                if (!String.IsNullOrEmpty(o as String))
                    return false;

            return true;
        }
    }
}
